﻿using System;

namespace Mbl.ParameterizedTest.FluentCollectionBuilding.Expectors.GeneralExpectations
{
    public class ExceptionExpectation<T, TException> : IExpectation<T>
        where TException : Exception
    {
        public AssertionResult HoldsFor(Func<T> actual)
        {
            try
            {
                actual();
                return AssertionResult.Error(
                    $"Expected an exception of type {typeof (TException).Name}, but no exceptions where thrown.");
            }
            catch (TException ex) when(typeof(TException) == ex.GetType())
            {
                return AssertionResult.Success;
            }
            catch (Exception ex)
            {
                return AssertionResult.Error(
                     $"Expected an exception of type <{typeof(TException).Name}>. Actual <{ex.GetType().Name}>.");
            }
        }
    }
}
